//
//  ZYChartView.m
//  Tourism
//
//  Created by 张亚 on 16/1/26.
//  Copyright © 2016年 zhangya. All rights reserved.
//

#import "ZYChartView.h"
@interface ZYChartView()
{
    NSMutableArray * pointsArray;
}
@end

@implementation ZYChartView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        pointsArray = [NSMutableArray array];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext(); //获取画布
    
    CGRect healthRect = CGRectMake(SCREEN_WIDTH/2 - 50 , 0, 100, 30);
    CGContextAddRect(context, healthRect);
    CGContextStrokePath(context);
    NSString * timeStr = @"健康状况";
    NSMutableDictionary * timeDict = [NSMutableDictionary dictionary];
    timeDict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    [timeStr drawInRect:healthRect withAttributes:timeDict];
    
    //画x轴
    CGContextMoveToPoint(context, 10, 280);
    CGContextAddLineToPoint(context, SCREEN_WIDTH - 20, 280);
    CGContextDrawPath(context, kCGPathFillStroke);
    //画y轴
    CGContextMoveToPoint(context, 10, 20);
    CGContextAddLineToPoint(context, 10, 280);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    
    CGRect cirRect = CGRectMake(20, 20, 50, 50);
    CGContextAddRect(context, cirRect);
    CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    
    
    
    CGContextMoveToPoint(context, 200, 100);
    CGContextAddLineToPoint(context, 100, 100);
    CGContextAddLineToPoint(context, 100, 200);
    CGContextAddArc(context, 100, 100, 100, 0, M_PI * 0.5, 0);
    CGContextDrawPath(context, kCGPathFill);
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //1、获取画布
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);// 设置画笔的颜色
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);//
    CGContextSetLineWidth(context, 3);//设置画线的宽度
    //2、绘画内容
    if([pointsArray count] > 1 ){
        CGPoint startPoint = [[pointsArray objectAtIndex:0] CGPointValue];
        CGContextMoveToPoint(context, startPoint.x,startPoint.y);
        for(int  i =1 ; i < pointsArray.count ;i ++){
            CGPoint startPoint = [[pointsArray objectAtIndex:i] CGPointValue];
            CGContextAddLineToPoint(context, startPoint.x, startPoint.y);
        }
    }
    //3、绘画路径
    CGContextDrawPath(context, kCGPathFillStroke);
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    NSSet *allTouches = [event allTouches];    //返回与当前接收者有关的所有的触摸对象
    UITouch *touch = [allTouches anyObject];   //视图中的所有对象
    CGPoint point = [touch locationInView:[touch view]]; //返回触摸点在视图中的当前坐标
    [pointsArray addObject:[NSValue valueWithCGPoint:point]];
    
    [self setNeedsDisplay];
}



@end
